package com.swingson.properties.impl;

import com.swingson.SwingsonGui;
import com.swingson.SwingsonGuiBuilder;
import com.swingson.exceptions.SwingsonException;
import com.swingson.jsonparser.FieldValuePair;
import com.swingson.jsonparser.JsonElement;
import com.swingson.jsonparser.JsonObject;
import com.swingson.properties.BasePropertyProcessor;
import java.awt.Color;
import javax.swing.JComponent;
import javax.swing.border.TitledBorder;

/**
 * @author sad
 */
public class TitleBorderPropertyProcessor extends BasePropertyProcessor {

    @Override
    public Object processProperty(JsonElement jsonElement, JComponent component, SwingsonGui gui, String propertyName) {
        if (jsonElement.isPrimitive()) {
            component.setBorder(new TitledBorder(jsonElement.getAsString()));
        } else {
            if (!jsonElement.isObject()) {
                throw new SwingsonException("TitleBorder should be of JsonString type or JsonObject, but found " + jsonElement.getClass().getSimpleName());
            }
            JsonObject obj = jsonElement.getAsObject();
            if (obj.getElementByName("text") == null) {
                throw new SwingsonException("TitleBorder should contain 'text' property");
            }
            String text = obj.getElementByName("text").getAsString();
            TitledBorder border = new TitledBorder(text);
            for (FieldValuePair fv : obj.getElements()) {
                if (fv.getName().equals("text")) {
                    continue;
                } else if (fv.getName().equals("position")) {
                    String position = fv.getValue().getAsString().toUpperCase();
                    int titlePosition = 0;
                    switch (position) {
                        case "ABOVE_TOP":
                            titlePosition = TitledBorder.ABOVE_TOP;
                            break;
                        case "TOP":
                            titlePosition = TitledBorder.TOP;
                            break;
                        case "BELOW_TOP":
                            titlePosition = TitledBorder.BELOW_TOP;
                            break;
                        case "ABOVE_BOTTOM":
                            titlePosition = TitledBorder.ABOVE_BOTTOM;
                            break;
                        case "BOTTOM":
                            titlePosition = TitledBorder.BOTTOM;
                            break;
                        case "BELOW_BOTTOM":
                            titlePosition = TitledBorder.BELOW_BOTTOM;
                            break;
                    }

                    border.setTitlePosition(titlePosition);
                } else if (fv.getName().equals("justification")) {
                    String position = fv.getValue().getAsString().toUpperCase();
                    int justification = 0;
                    switch (position) {
                        case "LEFT":
                            justification = TitledBorder.LEFT;
                            break;
                        case "CENTER":
                            justification = TitledBorder.CENTER;
                            break;
                        case "LEADING":
                            justification = TitledBorder.LEADING;
                            break;
                        case "TRAILING":
                            justification = TitledBorder.TRAILING;
                            break;
                    }

                    border.setTitleJustification(justification);
                } else if (fv.getName().equals("color")) {
                    Color color = (Color) SwingsonGuiBuilder.getPropertiesProcessorResolver().getTypeProcessingPropertyProcessor(Color.class).processProperty(fv.getValue(), component, gui, propertyName);
                    border.setTitleColor(color);
                }
            }

            component.setBorder(border);
        }

        return null;
    }

}
